using System.IO;
using EnvDTE;

namespace Core
{
	public class CodeDocument
	{
		private readonly Document _document;

		public CodeDocument(Document document)
		{
			_document = document;
		}

		public Document DteDocument
		{
			get { return _document; }
		}

		public ProgrammingLanguage Language
		{
			get
			{
				switch (Path.GetExtension(FilePath))
				{
					case ".h":
					case ".cpp":
						return ProgrammingLanguage.CPlusPlus;

					case ".cs":
						return ProgrammingLanguage.CSharp;
				}

				return ProgrammingLanguage.None;
			}
		}

		public string Name
		{
			get { return _document.Name; }
		}

		public string FilePath
		{
			get { return _document.FullName; }
		}

		public string DirectoryPath
		{
			get { return Path.GetDirectoryName(_document.FullName); }
		}

		public bool Saved
		{
			get { return _document.Saved; }
		}

		public EditPoint ActiveEditPoint
		{
			get { return ((TextSelection) _document.Selection).ActivePoint.CreateEditPoint(); }
		}

		public void GotoNextCamelCaseWord(bool extendSelection)
		{
			var selection = (TextSelection) _document.Selection;

			EditPoint searchPoint = selection.ActivePoint.CreateEditPoint();
			EditPoint result = EditPointHelpers.FindNextCamelCaseWord(searchPoint);

			selection.MoveToPoint(result, extendSelection);
		}

		public void GotoPreviousCamelCaseWord(bool extendSelection)
		{
			var selection = (TextSelection) _document.Selection;

			EditPoint searchPoint = selection.ActivePoint.CreateEditPoint();
			EditPoint result = EditPointHelpers.FindPreviousCamelCaseWord(searchPoint);

			selection.MoveToPoint(result, extendSelection);
		}

		public void InsertText(string text)
		{
			var selection = (TextSelection) _document.Selection;
			var editPoint = selection.ActivePoint.CreateEditPoint();

			editPoint.Insert(text);
		}
	}
}